package com.dl.exam.controller.user;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dl.exam.annotation.MethodInfo;
import com.dl.exam.entity.Exam;
import com.dl.exam.entity.Page;
import com.dl.exam.entity.question.Question;
import com.dl.exam.entity.question.SingleQuestion;
import com.dl.exam.service.ExamService;
import com.dl.exam.service.SingleService;
import com.dl.exam.service.SubjectiveService;
import com.dl.exam.service.impl.SingleServiceImpl;
import com.dl.exam.service.impl.SubjectiveServiceImpl;
import com.dl.exam.util.UUIDUtils;
import com.dl.exam.util.result.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * @author DL
 * @create 2022-04-22 14:13
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private ExamService examService;
    @Resource
    private SingleService singleService;
    @Resource
    private SubjectiveService subjectiveService;

    //查询用户所有的考试
    @GetMapping("/historyExam")
    private Result getHistoryExam(Integer currentPage) {
        Page<Exam> page = examService.getAllExam(StpUtil.getLoginIdAsString(), currentPage, 12);

        if (page != null){
            return Result.ok("成功").data("page", page);
        }else {
            return Result.error("失败");
        }
    }

    //查询用户已完成的考试信息和答案|
    @GetMapping("/getFinishedExam")
    private Result getFinishedExam(String examId, Integer examType, Integer currentPage) {
        Page<?> page = null;
        switch (examType){
            case 1 : page = singleService.getFinishedExamAnswer(examId, currentPage, 10);break;
            //case 2 : singleService.getFinishedExamAnswer(examId, currentPage, 10);break;
            case 3 : page = subjectiveService.getFinishedExamAnswer(examId, currentPage, 10);break;
            default:
                throw new IllegalStateException("Unexpected value: " + examType);
        }

        if (page != null){
            return Result.ok("成功").data("page", page);
        }else {
            return Result.no("失败");
        }
    }

    //region 查询用户未完成的考试
    @GetMapping("/getUnfinishedExam")
    private Result getUnfinishedExam() {
        Exam unfinishedExam = examService.getUnfinishedExam(StpUtil.getLoginIdAsString());
        if (unfinishedExam != null){
            Integer examType = unfinishedExam.getExamType();
            List<?> list = null;
            switch (examType){
                case 1 :
                    list = singleService.getUnfinishedQuestion(unfinishedExam.getExamId());break;
                //case 2 : singleService.getFinishedExamAnswer(examId, currentPage, 10);break;
                case 3 : list = subjectiveService.getUnfinishedQuestion(unfinishedExam.getExamId());break;
            }
            return Result.ok("成功").data("list", list).data("exam", unfinishedExam);
        }else {
            return Result.no("当前没有考试");
        }
    }

    @PostMapping("/startExam")
    private Result startExam(@RequestBody JSONObject jsonObject)  {
        JSONObject choiceForm = jsonObject.getJSONObject("choiceForm");
        String subject = choiceForm.getString("subject");// 科目
        Integer examType = choiceForm.getInteger("examType");// 问题类型
        Integer quantity = choiceForm.getInteger("quantity");// 数量
        Integer degree = choiceForm.getInteger("degree");// 难度

        String userId = StpUtil.getLoginIdAsString();
        Exam unfinishedExam = examService.getUnfinishedExam(userId);//查询是否存在考试
        if (unfinishedExam == null){
            List<?> questionList = null;
            switch (examType){//判断考试类型
                case 1 :// 单选题考试
                    questionList = singleService.getRandomQuestionByCondition(jsonObject);break;
                case 2 :// 判断题考试
                    break;
                case 3 :// 主观题考试
                    questionList = subjectiveService.getRandomQuestionByCondition(jsonObject);break;
            }
            System.out.println("questionList = " + questionList);
            if (questionList.size() < quantity) return Result.no("试题信息有误，题库数量不够！");

            List<String> questionIdList = questionList.stream()
                    .map(item -> { Question question = (Question) item;return question.getQuestionId();})
                    .collect(Collectors.toList());

            String examId = UUIDUtils.getUUID10();
            boolean startExam = examService.startExam(examId, userId, examType, questionIdList);
            if (startExam){
                return Result.ok("成功");
            }
            return Result.no("开始考试失败");
        }
        return Result.no("已经存在考试");
    }



    //提交考试
    @PostMapping("/submitExam")
    private Result submitExam(@RequestBody JSONObject jsonObject)  {
        boolean submitExam = examService.submitExam(jsonObject);
        if (submitExam){
            return Result.ok("成功");
        }else {
            return Result.no("失败");
        }

    }
}
